package mobi.weq.servlets.observation;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mobi.weq.dao.UserFeedbackDao;
import mobi.weq.model.UserFeedback;

/**
 * Servlet implementation class UserFeedback
 */
@WebServlet("/observation/feedback")
public class UploadUserFeedback extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// adjust user input using his settings

		String toleranceLevelString = request.getParameter("feedback");
		String latString = request.getParameter("latitude");
		String lngString = request.getParameter("longitude");
		
		
		UserFeedback uf = new UserFeedback();
		uf.setLatitude(Double.valueOf(latString));
		uf.setLongitude(Double.valueOf(lngString));
		uf.setToleranceLevel(Integer.parseInt(toleranceLevelString));
		uf.setTimestamp(new Date());

		UserFeedbackDao dao = new UserFeedbackDao();
		try {
			dao.addFeedback(uf);
		} catch (SQLException e) {
			e.printStackTrace();
		}
//		response.sendRedirect("../feedback");
		response.getWriter().print("{\"message\":\"Updated Successfully\"}");
	}
}
